int CLK_PIN = 2;
int IN_PIN = 4;
int OUT_PIN = 6;
int LED_PIN = 13;

int DELAY = 60;


byte mask = 1;

void setup(){


  Serial.begin(9600); 


  Serial.println("SuperMario"); 


  /* Setup pins */
  pinMode(CLK_PIN, OUTPUT);                                               
  pinMode(OUT_PIN, OUTPUT);                                              
  pinMode(IN_PIN, INPUT); 
  
  
     digitalWrite(IN_PIN, HIGH);                   // make sure the puillup resistors are off


  /* Reset clock */
  digitalWrite(CLK_PIN, HIGH);  
}

void loop(){


  /* Sample package */
  writeByte(0x88); // Sync #1
  writeByte(0x33); // Sync #2

  writeByte(0x0F); // Command Type Enquiry
  writeByte(0x00); // Compression. Off

  writeByte(0x00);
  writeByte(0x00);

  writeByte(0x0F);
  writeByte(0x00);

  writeByte(0x00);  
  writeByte(0x00); // Provoke ACK
 




  /* Try to read ack */
  byte value1 = 0;

  for(int n = 7; n>=0; n--){
    // delayMicroseconds(DELAY);
  //  digitalWrite(CLK_PIN, LOW);
   // delayMicroseconds(DELAY);

    if( digitalRead(IN_PIN) == HIGH ){
      value1 |= (1 << n);       // forces nth bit of x to be 1.  all other bits left alone.

    }
    else {
      value1 &= ~(1 << n);      // forces nth bit of x to be 0.  all other bits left alone.

    }

  //  digitalWrite(CLK_PIN, HIGH);
        delayMicroseconds(DELAY);

  }



  /* Try to read ack */
  byte value2 = 0;

  for(int n = 7; n>=0; n--){
    //delayMicroseconds(DELAY);
   // digitalWrite(CLK_PIN, LOW);
    // delayMicroseconds(DELAY);

    if( digitalRead(IN_PIN) == HIGH ){
      value2 |= (1 << n);       // forces nth bit of x to be 1.  all other bits left alone.

    }
    else {
      value2 &= ~(1 << n);      // forces nth bit of x to be 0.  all other bits left alone.
    }
    
  //  digitalWrite(CLK_PIN, HIGH);
        delayMicroseconds(DELAY);



  }

  Serial.println(value1, HEX);
  Serial.println(value2, HEX);
  Serial.println("---");

//  digitalWrite(CLK_PIN, HIGH);






  delay(1000);

}

void writeInt(unsigned int i){
  //Serial.println("Writing int:");

  writeByte((byte)(i));   // LsB
  // Serial.println((byte)(i), BIN);
  writeByte((byte)(i>>8));   // MsB
  //   Serial.println((byte)(i>>8), BIN);
}

void oldWriteByte(byte b){

  for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask
    if (b & mask){ // if bitwise AND resolves to true
      digitalWrite(OUT_PIN,LOW); // send 1
    }
    else{ //if bitwise and resolves to false
      digitalWrite(OUT_PIN,HIGH); // send 0
    }


    digitalWrite(CLK_PIN, LOW);
    // delayMicroseconds(DELAY);
    //Serial.print(digitalRead(IN_PIN));
    digitalWrite(CLK_PIN, HIGH);
    // delayMicroseconds(DELAY);


  }

}

void writeByte(byte b)
{
  for(int i=0;i<8;i++) {  //NEWS FLASH: there are 8 bits in a byte
    digitalWrite(CLK_PIN,LOW);                            //Set the clock to zero, wes about to send you somethin
    if(b & 0x80) {                                   //Magic
      digitalWrite(OUT_PIN,HIGH);                     //1
    } else {
      digitalWrite(OUT_PIN,LOW);                      //0
    }
    b <<= 1;            //More magic
    delayMicroseconds(DELAY);
    digitalWrite(CLK_PIN,HIGH);                           //Make the clock 1, so we can make a funky squarewave
          delayMicroseconds(DELAY);
  }
  digitalWrite(OUT_PIN,LOW);                          //We are done, turn the serial off

}

void otherWriteByte(byte b)
{
  for(int i=0;i<8;i++) {  //NEWS FLASH: there are 8 bits in a byte
    digitalWrite(CLK_PIN,LOW);                            //Set the clock to zero, wes about to send you somethin
    if(b & 0x01) {                                   //Magic
      digitalWrite(OUT_PIN,HIGH);                     //1
    } else {
      digitalWrite(OUT_PIN,LOW);                      //0
    }
    b >>= 1;            //More magic
    delayMicroseconds(DELAY);
    digitalWrite(CLK_PIN,HIGH);                           //Make the clock 1, so we can make a funky squarewave
          delayMicroseconds(DELAY);
  }
  digitalWrite(OUT_PIN,LOW);                          //We are done, turn the serial off

}



